package lib.self.ex.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

import lib.self.R;
import lib.self.ex.interfaces.IInitialize;
import lib.self.ex.interfaces.IOption;
import lib.self.util.autoFit.FitViewParamsUtil;
import lib.self.util.view.ViewUtil;
import lib.self.utils.ToastUtil;

/**
 * description: 对话框
 * author: Steven Xiao
 * date: 2017/5/3
 */
public abstract class DialogEx implements OnClickListener, IInitialize, IOption {

    protected String TAG = getClass().getSimpleName();

    private OnClickListener mDismissLsn;

    private Dialog mDialog;

    protected View mContentView;
    private Context mContext;

    public DialogEx(@Nullable Context context) {
        this.mContext = context;
        init();
    }

    private void init() {
        mDialog = new Dialog(mContext, R.style.DialogStyle);
        mContentView = getLayoutInflater().inflate(getContentViewId(), null);

        initData();
        findViews();
        setViewsValue();

        LayoutParams params = getParams();
        if (params != null) {
            mDialog.setContentView(mContentView, params);
        } else {
            mDialog.setContentView(mContentView);
        }

        FitViewParamsUtil.autoFitAll(mContentView);
    }

    @Override
    public int getContentHeaderViewId() {
        return 0;
    }

    /**
     * 调整宽高在这里处理
     */
    @Nullable
    protected LayoutParams getParams() {
        return null;
    }

    /**
     * @param type 如果type是 {@link WindowManager.LayoutParams#TYPE_SYSTEM_ALERT}
     *             需要添加权限 <p>
     *             permission:android.permission.SYSTEM_ALERT_WINDOW
     */
    public void setType(int type) {
        mDialog.getWindow().setType(type);
    }

    public void setCancelable(boolean flag) {
        mDialog.setCancelable(flag);
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
    }

    public void setOnCancelListener(OnCancelListener listener) {
        mDialog.setOnCancelListener(listener);
    }

    public void setOnDismissListener(OnDismissListener listener) {
        mDialog.setOnDismissListener(listener);
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    protected View findViewById(@IdRes int id) {
        return mContentView.findViewById(id);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 设置点击可以关闭dialog的view
     */
    protected void setDismissClicker(View view) {
        if (view == null) {
            return;
        }

        if (mDismissLsn == null) {
            mDismissLsn = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            };
        }
        view.setOnClickListener(mDismissLsn);
    }

    /**
     * 通过id设置点击可以关闭dialog的view
     */
    protected void setDismissClicker(@IdRes int id) {
        if (mDismissLsn == null) {
            mDismissLsn = v -> dismiss();
        }

        View view = findViewById(id);
        if (view != null) {
            view.setOnClickListener(mDismissLsn);
        }
    }

    @Override
    public void initTitleBar() {
        //无
    }

    @Override
    public void showView(View view) {
        ViewUtil.showView(view);
    }

    @Override
    public void hideView(View view) {
        ViewUtil.hideView(view);
    }

    @Override
    public void goneView(View view) {
        ViewUtil.goneView(view);
    }

    @Override
    public void startActivity(Class<? extends Activity> cls) {
        mContext.startActivity(new Intent(mContext, cls));
    }

    @Override
    public void startActivityForResult(Class<? extends Activity> cls, int requestCode) {

    }

    protected void startActivity(Intent intent) {
        mContext.startActivity(intent);
    }

    /**
     * 设置空白处黑暗度
     *
     * @param amount 0-1.0 0为全透明 1为全黑
     */
    protected void setDimAmount(float amount) {
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.dimAmount = amount;
        mDialog.getWindow().setAttributes(params);
        mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    /**
     * 设置对齐方式，默认居中
     *
     * @param gravity
     */
    protected void setGravity(int gravity) {
        mDialog.getWindow().setGravity(gravity);
    }

    /**
     * 设置弹出动画方式
     *
     * @param dialogStyleId
     */
    protected void setAnimation(@StyleRes int dialogStyleId) {
        mDialog.getWindow().setWindowAnimations(dialogStyleId);
    }

    protected void setOnClickListener(@IdRes int resId) {
        View view = findViewById(resId);
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    protected void showToast(String text) {
        ToastUtil.showToast(mContext, text);
    }

    protected void showToast(@StringRes int resId) {
        ToastUtil.showToast(mContext, resId);
    }

    protected Context getCOntext() {
        return mContext;
    }

    protected View getContentView() {
        return mContentView;
    }

    protected LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(mContext);
    }
}
